<template>
  <a-card :bordered="false">
    <!-- 查询区域 -->
    <search :config="searchConfig" @search="search" @getHeight="getHeight"/>
    <div class="page_table">
      <vxe-grid
        ref="xGrid"
        :border="true"
        :resizable="true"
        :showOverflow="true"
        size="mini"
        :loading="loading"
        :height="pageHeight-seachHeight-90"
        :align="left"
        :columns="columns"
        :data="dataSource"
        :toolbar-config="tableToolbar"
        :checkbox-config="checkboxConfig"
        v-bind="vexConfig"
        @checkbox-change="selectChangeEvent"
        @checkbox-all="selectChangeEvent"
      >
        <template #toolbar_buttons>
          <div class="table-operator">
            <a-button v-has="'userorgrel:add'" @click="handleAdd">新增</a-button>
            <a-button v-has="'userorgrel:delete'" @click="batchDel">删除</a-button>
            <a-button v-has="'userorgrel:synchronization'" @click="">同步</a-button>
            <a-button v-has="'userorgrel:export'" @click="">导出</a-button>
          </div>
        </template>
        <template #userCode="{ row,rowIndex }">
          <a @click="handleSelect(row)">{{ row.userCode }}</a>
        </template>
        <template #detail="{ row,rowIndex }">
          <a @click="handleDetail(row)">{{ row.provinceName }}</a>
        </template>
        <template #status="{ row }">
          <a-switch :checked="row.status" size="small" @change="changeStatus(row,$event)"/>
        </template>

        <template #action="{ row:record }">
          <a v-has="'userorgrel:update'" @click="handleEdit(record)">编辑</a>
          <a-divider type="vertical"/>
          <a v-has="'business:log'" href="javascript:;" @click='handleBusiness("用户组织关系",row)'>日志</a>
        </template>

        <template #pager>
          <vxe-pager
            :layouts="['Total', 'PrevJump', 'PrevPage', 'Number', 'NextPage', 'NextJump','Sizes', 'FullJump']"
            :currentPage.sync="ipagination.current"
            :page-size.sync="ipagination.pageSize"
            :total="ipagination.total"
            :pageSizes="ipagination.pageSizeOptions"
            @page-change="handlePageChange">
          </vxe-pager>
        </template>
      </vxe-grid>
    </div>
    <business-log ref="businessLog"></business-log>
    <userorgerlModal ref="modalForm" @ok="modalFormOk"></userorgerlModal>
  </a-card>
</template>

<script>
import userorgerlModal from './modules/UserorgerlModal.vue'
import { PageListMixin } from '@/mixins/PageListMixin'
import { getAction, postAction } from '@api/manage'
import Vue from 'vue'

export default {
  name: 'userorgerl',
  mixins: [PageListMixin],
  components: {
    userorgerlModal
  },
  data() {
    return {
      selectedRowKeys: [],
      description: '用户组织关系',
      searchConfig: [
        {
          type: 'input',
          name: 'userCode',
          placeholder: '用户编号',
        }, {
          type: 'input',
          name: 'userName',
          placeholder: '用户名称'
        },
        {
          // label:'区域编号',
          type: 'input',
          name: 'orgCode',
          placeholder: '组织编号'
        },
        {
          type: 'j-select',
          dictCode: 'user_status',
          name: 'status',
          placeholder: '状态'
        }
      ],
      //表头
      columns: [],
      //列设置
      settingColumns: [],
      defColumns: [
        { type: 'checkbox', width: 60, fixed: 'left' },
        {
          fixed: 'left',
          title: '用户编号',
          minWidth: 100,
          field: 'userCode',
          slots: { default: 'userCode' }

        },
        {
          title: '用户名称',
          minWidth: 100,
          field: 'userName',
          ellipsis: true
        },
        {
          title: '组织编号',
          minWidth: 100,
          field: 'orgCode',
          ellipsis: true
        },
        {
          title: '组织名称',
          minWidth: 100,
          field: 'orgName',
          ellipsis: true
        },
        {
          title: '备注',
          minWidth: 100,
          field: 'remarks',
          ellipsis: true
        },
        {
          title: '状态',
          align: 'center',
          field: 'status',
          fixed: 'right',
          minWidth: 100,
          slots: {
            default: 'status'
          }
        },
        {
          title: '创建人',
          align: 'left',
          minWidth: 80,
          field: 'createName',
        },
        {
          title: '创建时间',
          align: 'left',
          minWidth: 80,
          field: 'createTime',
        },
        {
          title: '更新人',
          align: 'left',
          minWidth: 80,
          field: 'updateName',
        },
        {
          title: '更新时间',
          align: 'left',
          minWidth: 80,
          field: 'updateTime',
        },
        {
          title: '操作',
          field: 'action',
          align: 'center',
          fixed: 'right',
          width: 120,
          minWidth: 100,
          slots: {
            default: 'action'
          }
        }
      ],
      url: {
        list: window._CONFIG['domianURL'] + '/userorgrelation/cnUserOrgRelation/findPageList',
        detail: window._CONFIG['domianURL'] + '/customergroup/cnCustomerGroup/getInfo',
        edit: window._CONFIG['domianURL'] + '/mdsProduct/saveOrUpdateEntity',
        deleteBatch: '/userorgrelation/cnUserOrgRelation/deleteBatch',
        delete: window._CONFIG['domianURL'] + '/userorgrelation/cnUserOrgRelation/delete',
        exportXlsUrl: window._CONFIG['domianTpmURL'] + 'budget/tpmBudgetFee/exportXls',
        startOrStopUrl: window._CONFIG['domianURL'] + '/userorgrelation/cnUserOrgRelation/updateStatus',
      }
    }
  },
  computed: {
    importExcelUrl: function () {
      return `${window._CONFIG['domianURL']}/${this.url.importExcelUrl}`
    }
  },
  created() {
    this.initColumns()
  },
  methods: {
    //列表设置更改事件
    onColSettingsChange(checkedValues) {
      var key = this.$route.name + ':colsettings'
      Vue.ls.set(key, checkedValues, 7 * 24 * 60 * 60 * 1000)
      this.settingColumns = checkedValues
      const cols = this.defColumns.filter(item => {
        if (item.key == 'rowIndex' || item.field == 'action') {
          return true
        }
        if (this.settingColumns.includes(item.field)) {
          return true
        }
        return false
      })
      this.columns = cols
    },
    onSelectChange(selectedRowKeys, selectionRows) {
      this.selectedRowKeys = selectedRowKeys
      this.selectionRows = selectionRows
    },
    onClearSelected() {
      this.selectedRowKeys = []
      this.selectionRows = []
    },
    changeStatus(row, status) {
      let that = this
      this.loading = true
      getAction(that.url.startOrStopUrl, { id: row.id, status: Number(status) }).then((res) => {
        if (res.success) {
          that.$message.success(res.message)
          that.loadData()
          that.onClearSelected()
        } else {
          that.$message.warning(res.message)
        }
      }).finally(() => {
        that.loading = false
      })
    },
    handleSelect: async function (row) {
      this.$refs.modalForm.edit(row)
      this.$refs.modalForm.title = '详情'
      this.$refs.modalForm.disableSubmit = true
    },
    handleEdit: async function (row) {
      this.$refs.modalForm.title = '编辑'
      this.$refs.modalForm.edit(row)
      this.$refs.modalForm.disableSubmit = false
    },
    initColumns() {
      //权限过滤（列权限控制时打开，修改第二个参数为授权码前缀）
      //this.defColumns = colAuthFilter(this.defColumns,'testdemo:');

      var key = this.$route.name + ':colsettings'
      let colSettings = Vue.ls.get(key)
      if (colSettings == null || colSettings == undefined) {
        let allSettingColumns = []
        this.defColumns.forEach(function (item, i, array) {
          allSettingColumns.push(item.field)
        })
        this.settingColumns = allSettingColumns
        this.columns = this.defColumns
      } else {
        this.settingColumns = colSettings
        const cols = this.defColumns.filter(item => {
          if (item.key == 'rowIndex' || item.field == 'action') {
            return true
          }
          if (colSettings.includes(item.field)) {
            return true
          }
          return false
        })
        this.columns = cols
      }
    },
  }
}
</script>

<style scoped>
@import '~@assets/less/common.less';
</style>